package com.QiHunCn.app;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.net.Uri;

/**
 * Created by liguifu on 14-1-17.
 *
 * A placeholder fragment containing a simple view.
 */
public  class PlaceholderFragment extends Fragment{


    OnPlaceholderFragmentButtomClickListener mSndPoolListener;
    private Bitmap mBitmapIn;
    private ImageView mImageView;
    private ImageButton btnPre, btnNext;



    public PlaceholderFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//            View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        // image_detail_fragment.xml contains just an ImageView
        final View v = inflater.inflate(R.layout.fragment_main, container, false);
        mImageView = (ImageView) v.findViewById(R.id.imgWhite);
        btnPre = (ImageButton)v.findViewById(R.id.imageButtonPre);
        btnNext = (ImageButton)v.findViewById(R.id.imageButtonNext);
        return v;//rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //mImageView.setImageResource(R.drawable.ic_launcher); // Load image into ImageView
        mBitmapIn = loadBitmap(R.drawable.white_center);
        mImageView.setImageBitmap(mBitmapIn);
        btnPre.setImageBitmap(loadBitmap(R.drawable.arrow_left));
        btnNext.setImageBitmap(loadBitmap(R.drawable.arrow_right));



        btnPre.setOnClickListener(new ImageButton.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                mSndPoolListener.OnPlaceholderFragmentButtomClicked(btnPre.getId());
            }
        });

        btnNext.setOnClickListener(new ImageButton.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                mSndPoolListener.OnPlaceholderFragmentButtomClicked(btnNext.getId());
            }
        });

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mSndPoolListener = (OnPlaceholderFragmentButtomClickListener)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnPlaceholderFragmentButtomClickListener");
        }
    }

    // Container Activity must implement this interface
    public interface OnPlaceholderFragmentButtomClickListener {
        public void OnPlaceholderFragmentButtomClicked(Integer id);
    }

    private Bitmap loadBitmap(int resource) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeResource(getResources(), resource, options);
    }
}


